package cn.rokhdelar.umbrsapi.common.entity;

import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;

@Data
public class R<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    private HttpStatus code;
    private String msg;
    private T data;
    private LocalDateTime localDateTime;

    public R(HttpStatus code, String msg) {
        this.code = code;
        this.msg = msg;
        this.localDateTime = LocalDateTime.now();
    }

    public R(HttpStatus code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.localDateTime = LocalDateTime.now();
    }

    public static R<Void> ok() {
        return new R<>(HttpStatus.OK, "成功。");
    }

    public static <T> R<T> ok(T data) {
        return new R<>(HttpStatus.OK, "成功。", data);
    }

    public static <T> R<T> fail() {
        return new R<>(HttpStatus.INTERNAL_SERVER_ERROR, "内部服务器错误。");
    }

    public static <T> R<T> fail(HttpStatus code, String msg) {
        return new R<>(code, msg);
    }

    public static <T> R<T> fail(HttpStatus code, String msg, T data) {
        return new R<>(code, msg, data);
    }
}
